package com.hxexp.weixin.mvc.refund.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hxexp.weixin.common.exception.ServiceException;
import com.hxexp.weixin.hpe.api.returnamount.model.ReturnAmountResultBean;
import com.hxexp.weixin.mvc.refund.model.RefundInfoBean;
import com.hxexp.weixin.mvc.refund.model.TicketInfoBean;
import com.hxexp.weixin.mvc.refund.model.TicketInfoParam;
import com.hxexp.weixin.mvc.refund.service.RefundService;

/**
 * 退票控制器
 *
 * @author wang.bin
 * @author li.jiang
 */
@Controller
@RequestMapping(value = "/refund")
public final class RefundController {

    private static final Logger LOGGER = LoggerFactory.getLogger(RefundController.class);

    @Autowired
    private RefundService refundService;

    /**
     * 查询可退金额
     * @param ticketinfoparam
     * @param model
     * @return
     */
    @RequestMapping(value = "/returnamount")
    @ResponseBody
    public ReturnAmountResultBean returnAmount(TicketInfoParam ticketinfoparam, Model model) {
        List<TicketInfoBean> list = ticketinfoparam.getTicketinfos();
        List<TicketInfoBean> result = new ArrayList<TicketInfoBean>();

        ReturnAmountResultBean returnAmountResult = new ReturnAmountResultBean();
        for(TicketInfoBean ticketInfoBean : list){
            if(ticketInfoBean.getCheckbox()!=null){
                result.add(ticketInfoBean);
            }
        }
        try {
            returnAmountResult = refundService.getReturnAmount(result);
        } catch (ServiceException e) {
            LOGGER.error("调用查询可退金额发生异常", e);
        }
        return returnAmountResult;
    }

    /**
     * 退票
     * @param ticketinfoparam
     * @param orderId
     * @param stationId
     * @param model
     * @return
     */
    @RequestMapping(value = "/refundticket")
    @ResponseBody
    public List<RefundInfoBean> refund(TicketInfoParam ticketinfoparam, String orderId, String stationId, Model model){
        List<TicketInfoBean> ticketInfoList = ticketinfoparam.getTicketinfos();
        List<TicketInfoBean> checkedTicketInfoList = new ArrayList<TicketInfoBean>();

        List<RefundInfoBean> resultlist = new ArrayList<RefundInfoBean>();
        for (TicketInfoBean ticketInfoBean : ticketInfoList) {
            if (ticketInfoBean.getCheckbox() != null) {
                checkedTicketInfoList.add(ticketInfoBean);
            }
        }

        try {
            resultlist = refundService.refund(checkedTicketInfoList, orderId, stationId);
        } catch (ServiceException e) {
            LOGGER.error("调用退票接口发生异常", e);
        }
        return resultlist;
    }
}
